package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {


    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 查询操作
     * @param geofenceQuery
     * @return
     */
    @Override
    public List<GeofenceVO> select(GeofenceQuery geofenceQuery) {
        log.debug("{}",geofenceQuery);
        List<GeofenceVO> list =  geofenceMapper.select(geofenceQuery);
        for (GeofenceVO geofenceVO : list){
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVO = vehicleMapper.list(vehicleQuery);
            //可用车辆个数
            int size=0;
            for (VehicleVO vehicleVO1 : vehicleVO){
                if (vehicleVO1.getStatus().equals("1")){
                    size++;
                }
            }
            geofenceVO.setVehicleList(vehicleVO);
            geofenceVO.setAvailableNum(size);
            geofenceVO.setTotalNum(vehicleVO.size());
        }
        return list;
    }

    @Override
    public List<VehicleVO> selectvehicle(VehicleQuery vehicleQuery) {
        List<VehicleVO> list = vehicleMapper.list(vehicleQuery);
        return list;
    }

    @Override
    public void updateVehicle(Long id) {
        log.debug("{}",id);
        Vehicle vehicle = new Vehicle();
        vehicle.setGeofenceBindStatus("0");
        vehicle.setId(id);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateId(vehicle);
    }

    @Override
    public void updateVehiclenew(Long id, Long geofenceId) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(geofenceId);
        vehicle.setGeofenceBindStatus("1");
        vehicleMapper.update(vehicle);
    }

    /**
     * 删除操作
     * @param id
     */
    @Override
    public void delete(Long id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setGeofenceId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateId(vehicle);
        geofenceMapper.delete(id);
    }

    @Override
    public void updataGeofence(Long id, String status) {
        Geofence geofence = new Geofence();
        geofence.setId(id);
        geofence.setStatus(status);
        geofence.setUpdateTime(new Date());
        geofenceMapper.update(geofence);
    }

    @Override
    public void Insert(GeofenceParam geofenceParam) {
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        geofence.setCreateTime(new Date());
        geofence.setStatus("1");
        geofenceMapper.insert(geofence);
    }
}
